﻿using System;
using System.Globalization;
using System.Windows.Forms;


public class DateTextBox
{
    public static string CS_DISPLAY_DATE_FORMAT = "dd/MM/yyyy";
    public static string CS_EDIT_DATE_FORMAT = "dd/MM/yyyy";

    public static System.DateTime Date_ParseEx(string sValue)
    {
        System.DateTime mDate = DateTime.MinValue;
        Date_TryParseEx(sValue, ref mDate);
        return mDate;
    }

    public static bool Date_TryParseEx(string sValue, ref System.DateTime dReturn)
    {
        if (System.DateTime.TryParseExact(sValue, CS_EDIT_DATE_FORMAT, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out dReturn))
        {
            return true;
        }
        else
        {
            if (System.DateTime.TryParse(sValue, out dReturn))
            {
                return true;
            }
        }
        return false;
    }

    public static void Date_FixString(ref string sValue)
    {
        System.DateTime tempresult1 = DateTime.MinValue;
        if (!(System.DateTime.TryParse(sValue, out tempresult1)) || sValue.Length > 4)
        {
            sValue = sValue.Replace(" ", "/");
            if (sValue.Length == 8)
            {
                sValue = sValue.Substring(0, 2) + "/" + sValue.Substring(2, 2) + "/" + sValue.Substring(4, 4);
            }
            if (sValue.Length == 6)
            {
                DateTime dt = DateTime.Parse("01/01/" + sValue.Substring(4, 2));
                sValue = sValue.Substring(0, 2) + "/" + sValue.Substring(2, 2) + "/" + dt.Year;  //+ sValue.Substring(4, 2);
            }
            else if (sValue.Length == 4)
            {
                sValue = sValue.Substring(0, 2) + "/" + sValue.Substring(2, 2);
                sValue = sValue + "/" + System.DateTime.Today.Year;
            }
            else if (sValue.Length == 5 && sValue.Substring(2, 1) == "/")
            {
                sValue = sValue + "/" + System.DateTime.Today.Year;

            }
            else if (sValue.Length == 2)
            {
                if (long.Parse(sValue) < 32)
                {
                    sValue += "/" + System.DateTime.Today.Month.ToString().PadLeft(2, '0');
                    sValue = sValue + "/" + System.DateTime.Today.Year;
                }
            }
        }
    }

    private TextBox objTextBox;
    private bool m_bClearError = true;

    protected DateTextBox()
    {

    }

    public bool AutoClearError
    {
        get
        {
            return m_bClearError;
        }
        set
        {
            m_bClearError = value;
        }
    }

    public string ControlName
    {
        get
        {
            return objTextBox.Name;
        }
    }

    public DateTextBox(TextBox tb)
    {
        this.objTextBox = tb;
        objTextBox.Enter += objTextBox_Enter;
        objTextBox.KeyPress += TextBox_KeyPress;
        objTextBox.MouseEnter += objTextBox_MouseEnter;
        objTextBox.MouseLeave += objTextBox_MouseLeave;
        objTextBox.TextChanged += objTextBox_TextChanged;
        objTextBox.Validating += TextBox_Validating;

        tb.MaxLength = 10;
        tb.CharacterCasing = CharacterCasing.Upper;
    }

    private void objTextBox_Enter(object sender, System.EventArgs e)
    {
        try
        {
            if (objTextBox.ReadOnly)
            {
                return;
            }
            DateTime value = DateTime.MinValue;
            if (Date_TryParseEx(objTextBox.Text, ref value))
            {
                objTextBox.Text = value.ToString(CS_EDIT_DATE_FORMAT);
            }

            objTextBox.SelectAll();
        }
        catch
        {
            //Message_Error(ex);
        }
    }

    private void TextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        try
        {
            if (e.Handled)
            {
                return;
            }

            bool bHandled = false;
            switch (System.Convert.ToInt32(e.KeyChar))
            {
                case 13: //Enter
                    if (!objTextBox.Multiline)
                    {
                        e.Handled = true;
                        SendKeys.SendWait("{TAB}");
                        return;
                    }
                    break;
                case 8: //Backspace
                    break;
                default:
                    string sValidChar = "0123456789/-";
                    if (sValidChar != "")
                    {
                        bHandled = (sValidChar.IndexOf(e.KeyChar) < 0);
                    }
                    break;
            }

            e.Handled = bHandled;

        }
        catch
        {
            //Throw ex
            //Message_Error(ex);
        }
    }

    private void objTextBox_MouseEnter(object sender, System.EventArgs e)
    {
        //m_bMouseOver = true;
    }

    private void objTextBox_MouseLeave(object sender, System.EventArgs e)
    {
        //m_bMouseOver = false;
    }

    private void objTextBox_TextChanged(object sender, System.EventArgs e)
    {
        //Control_SetError(objTextBox, "");
    }

    private void TextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        try
        {
            if (objTextBox.ReadOnly)
            {
                return;
            }

            string sValue = objTextBox.Text.Trim();

            bool bWrong = false;
            //string sMessage = "";

            if (sValue.Length > 0)
            {

                Date_FixString(ref sValue);
                System.DateTime mDate = DateTime.MinValue;
                if (!(Date_TryParseEx(sValue, ref mDate)))
                {
                    bWrong = true;
                }
                else
                {
                    //Object_SetValue(sValue, mDate)
                    objTextBox.Text = mDate.ToString(CS_DISPLAY_DATE_FORMAT); //CType(sValue, Date).ToString(cstDisplayDateFormat)
                }
                //sMessage = "Please input Date value";

                if (bWrong && m_bClearError)
                {
                    //Auto clear error
                    objTextBox.Text = "";
                    return;
                }
            }

            if (bWrong)
            {
                e.Cancel = true;
            }
            else
            {
                //sMessage = "";
            }

            //Control_SetError(objTextBox, sMessage);

        }
        catch
        {
            //Throw
            //Message_Error(ex);
        }
    }

}
public class DateTextBoxShort
{
    public static string CS_DISPLAY_DATE_FORMAT = "MM/yyyy";
    public static string CS_EDIT_DATE_FORMAT = "dd/MM/yyyy";

    public static System.DateTime Date_ParseEx(string sValue)
    {
        System.DateTime mDate = DateTime.MinValue;
        Date_TryParseEx(sValue, ref mDate);
        return mDate;
    }

    public static bool Date_TryParseEx(string sValue, ref System.DateTime dReturn)
    {
        if (System.DateTime.TryParseExact(sValue, CS_EDIT_DATE_FORMAT, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out dReturn))
        {
            return true;
        }
        else
        {
            if (System.DateTime.TryParse(sValue, out dReturn))
            {
                return true;
            }
        }
        return false;
    }

    public static void Date_FixString(ref string sValue)
    {
        System.DateTime tempresult1 = DateTime.MinValue;
        if (!(System.DateTime.TryParse(sValue, out tempresult1)) || sValue.Length > 4)
        {
            sValue = sValue.Replace(" ", "/");
            if (sValue.Length == 8)
            {
                sValue = sValue.Substring(0, 2) + "/" + sValue.Substring(2, 2) + "/" + sValue.Substring(4, 4);
            }
            if (sValue.Length == 6)
            {
                DateTime dt = DateTime.Parse("01/01/" + sValue.Substring(4, 2));
                sValue = sValue.Substring(0, 2) + "/" + sValue.Substring(2, 2) + "/" + dt.Year;  //+ sValue.Substring(4, 2);
            }
            else if (sValue.Length == 4)
            {
                sValue = sValue.Substring(0, 2) + "/" + sValue.Substring(2, 2);
                sValue = sValue + "/" + System.DateTime.Today.Year;
            }
            else if (sValue.Length == 5 && sValue.Substring(2, 1) == "/")
            {
                sValue = sValue + "/" + System.DateTime.Today.Year;

            }
            else if (sValue.Length == 2)
            {
                if (long.Parse(sValue) < 32)
                {
                    sValue += "/" + System.DateTime.Today.Month.ToString().PadLeft(2, '0');
                    sValue = sValue + "/" + System.DateTime.Today.Year;
                }
            }
        }
    }

    private TextBox objTextBox;
    private bool m_bClearError = true;

    protected DateTextBoxShort()
    {

    }

    public bool AutoClearError
    {
        get
        {
            return m_bClearError;
        }
        set
        {
            m_bClearError = value;
        }
    }

    public string ControlName
    {
        get
        {
            return objTextBox.Name;
        }
    }

    public DateTextBoxShort(TextBox tb)
    {
        this.objTextBox = tb;
        objTextBox.Enter += objTextBox_Enter;
        objTextBox.KeyPress += TextBox_KeyPress;
        objTextBox.MouseEnter += objTextBox_MouseEnter;
        objTextBox.MouseLeave += objTextBox_MouseLeave;
        objTextBox.TextChanged += objTextBox_TextChanged;
        objTextBox.Validating += TextBox_Validating;

        tb.MaxLength = 7;
        tb.CharacterCasing = CharacterCasing.Upper;
    }

    private void objTextBox_Enter(object sender, System.EventArgs e)
    {
        try
        {
            if (objTextBox.ReadOnly)
            {
                return;
            }
            DateTime value = DateTime.MinValue;
            if (Date_TryParseEx("01/" + objTextBox.Text, ref value))
            {
                objTextBox.Text = value.ToString(CS_DISPLAY_DATE_FORMAT);
            }

            objTextBox.SelectAll();
        }
        catch
        {
            //Message_Error(ex);
        }
    }

    private void TextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        try
        {
            if (e.Handled)
            {
                return;
            }

            bool bHandled = false;
            switch (System.Convert.ToInt32(e.KeyChar))
            {
                case 13: //Enter
                    if (!objTextBox.Multiline)
                    {
                        e.Handled = true;
                        SendKeys.SendWait("{TAB}");
                        return;
                    }
                    break;
                case 8: //Backspace
                    break;
                default:
                    string sValidChar = "0123456789/-";
                    if (sValidChar != "")
                    {
                        bHandled = (sValidChar.IndexOf(e.KeyChar) < 0);
                    }
                    break;
            }

            e.Handled = bHandled;

        }
        catch
        {
            //Throw ex
            //Message_Error(ex);
        }
    }

    private void objTextBox_MouseEnter(object sender, System.EventArgs e)
    {
        //m_bMouseOver = true;
    }

    private void objTextBox_MouseLeave(object sender, System.EventArgs e)
    {
        //m_bMouseOver = false;
    }

    private void objTextBox_TextChanged(object sender, System.EventArgs e)
    {
        //Control_SetError(objTextBox, "");
    }

    private void TextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        try
        {
            if (objTextBox.ReadOnly)
            {
                return;
            }
            string sValue = "";
            if (objTextBox.Text.Trim().Length > 0)
            {
                if (objTextBox.Text.Contains("/"))
                    sValue = "01/" + objTextBox.Text.Trim();
                else
                    sValue = "01" + objTextBox.Text.Trim();
            }
            bool bWrong = false;
            //string sMessage = "";

            if (sValue.Length > 0)
            {

                Date_FixString(ref sValue);
                System.DateTime mDate = DateTime.MinValue;
                if (!(Date_TryParseEx(sValue, ref mDate)))
                {
                    bWrong = true;
                }
                else
                {
                    //Object_SetValue(sValue, mDate)
                    objTextBox.Text = mDate.ToString(CS_DISPLAY_DATE_FORMAT); //CType(sValue, Date).ToString(cstDisplayDateFormat)
                }
                //sMessage = "Please input Date value";

                if (bWrong && m_bClearError)
                {
                    //Auto clear error
                    objTextBox.Text = "";
                    return;
                }
            }

            if (bWrong)
            {
                e.Cancel = true;
            }
            else
            {
                //sMessage = "";
            }

            //Control_SetError(objTextBox, sMessage);

        }
        catch
        {
            //Throw
            //Message_Error(ex);
        }
    }

}
